﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class PortManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem)
            {
                UxPort uxPort = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxPort;
                var portUpdate = uxPort.GetData();
                var port = this.DbContext.VIBA_PORTs.FirstOrDefault(u => u.Port_Id == portUpdate.Port_Id);
                if (port != null)
                {
                    port.Port_Number = portUpdate.Port_Number;
                    port.Card_Id = portUpdate.Card_Id;
                    port.Description = portUpdate.Description;
                    port.Status = portUpdate.Status;

                    this.DbContext.SubmitChanges();
                }

            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_PORTs.ToList();
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var portId = (e.Item as GridDataItem)["Port_Id"].Text;
                var port = this.DbContext.VIBA_PORTs.FirstOrDefault(u => u.Port_Id == Convert.ToInt32(portId));
                if (port != null)
                {
                    this.DbContext.VIBA_PORTs.DeleteOnSubmit(port);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var port = e.Item.DataItem as VIBA_PORT;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {
                UxPort uxPort = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxPort;
                uxPort.AddComboBoxItems();
                //uxPort.EnableFieldCode(true);
                uxPort.InsertOrUpdate = true;
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UxPort uxPort = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxPort;
                uxPort.AddComboBoxItems();
                uxPort.BindData(port);
                //uxPort.EnableFieldCode(false);
                uxPort.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem)
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);
                dataItem["Card_TenCard"].Text = GetCardName(port.Card_Id.ToString());
                
            }
        }

        private string GetCardName(string cardId)
        {
            if (cardId == null)
                cardId = "";
            var st = this.CardList.FirstOrDefault(x => x.Card_Id.ToString() == cardId.Trim());
            if (st != null)
                return st.Card_TenCard;
            return string.Empty;
        }

        private List<VIBA_CARD> _cardList;
        public List<VIBA_CARD> CardList
        {
            get
            {
                if (_cardList == null)
                    _cardList = new BTSModelDataContext().VIBA_CARDs.ToList();
                return _cardList;
            }
            set
            {
                _cardList = value;
            }
        }

        private string GetStatus(string status)
        {
            return status == "1" ? "<font color='blue'>Hoạt động</font>" : "<font color='red'>Không hoạt động</font>";
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxPort uxPort = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxPort;
                var port = uxPort.GetData();
                if (!IsExists(port.Port_Id.ToString()))
                {
                    this.DbContext.VIBA_PORTs.InsertOnSubmit(port);
                    this.DbContext.SubmitChanges();
                    uxPort.SetErrorStatus(string.Empty);
                }
                else
                {
                    uxPort.SetErrorStatus("Mã sự cố đã tồn tại, xin chọn mã khác.");
                    e.Canceled = true;
                }
            }
        }

        private bool IsExists(string problemId)
        {
            var context = new BTSModelDataContext();
            var item = context.VIBA_PORTs.FirstOrDefault(x => x.Port_Id == Convert.ToInt32(problemId.ToLower()));
            return item != null;
        }
    }
}